package com.bjc.base.config;

import java.io.Serializable;
import java.nio.charset.Charset;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisPoolingClientConfigurationBuilder;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;


/**
 * redis配置
 * 
 * @author 许自辉
 * @date 2017年7月20日 上午9:08:27
 */
@Configuration
@AutoConfigureAfter(RedisConfig.class)
public class RedisConfig {
	
	

	@SuppressWarnings("rawtypes")
	@Bean
	public RedisSerializer fastJson2JsonRedisSerializer() {
		return new FastJson2JsonRedisSerializer<Object>(Object.class);
	}

//	@Bean
//	public RedisConnectionFactory redisConnectionFactory() {
//		return redisConnectionFactorys();
//	}
	
	
    @Bean
    public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Serializable> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

	@Bean
	public RedisTemplate<Object, Object> redisTemplat2e(LettuceConnectionFactory redisConnectionFactory,
			@SuppressWarnings("rawtypes") RedisSerializer fastJson2JsonRedisSerializer) {
		RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(redisConnectionFactory);
		// 使用Jackson2JsonRedisSerialize 替换默认序列化
		// Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new
		// Jackson2JsonRedisSerializer<Object>(Object.class);
		// ObjectMapper objectMapper = new ObjectMapper();
		// objectMapper.setVisibility(PropertyAccessor.ALL,
		// JsonAutoDetect.Visibility.ANY);
		// objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		// jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
		// 设置value的序列化规则和 key的序列化规则
		redisTemplate.setValueSerializer(fastJson2JsonRedisSerializer);
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		redisTemplate.afterPropertiesSet();
		return redisTemplate;
	}

//	@Bean
//	public JedisPoolConfig getRedisConfig() {
//		JedisPoolingClientConfigurationBuilder config = new JedisPoolConfig();
//		config.setMaxIdle(maxIdle);
//		config.setMaxWaitMillis(maxWait);
////		config.setMaxTotal(1024);
//		config.setTestOnBorrow(true);
//		config.setTestOnReturn(true);
//		//Idle时进行连接扫描  
//		config.setTestWhileIdle(true);  
//		//表示idle object evitor两次扫描之间要sleep的毫秒数  
//		config.setTimeBetweenEvictionRunsMillis(30000);  
//		//表示idle object evitor每次扫描的最多的对象数  
//		config.setNumTestsPerEvictionRun(10);  
//		//表示一个对象至少停留在idle状态的最短时间，然后才能被idle object evitor扫描并驱逐；这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义  
//		config.setMinEvictableIdleTimeMillis(60000);  
//		return config;
//	}
	
//	@Bean
//	public JedisConnectionFactory redisConnectionFactorys() {
//		JedisConnectionFactory factory = new JedisConnectionFactory();
//		factory.setHostName(url);
//		factory.setPort(port);
//		factory.setPassword(password);
//		factory.setTimeout(timeout); // 设置连接超时时间
//		return factory;
//	}

//	@Bean
//	public JedisPool redisPoolFactory() {
//		JedisPool jedisPool = new JedisPool(getRedisConfig(), url, port,timeout,password);
//		return jedisPool;
//	}

	class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> {
		private Class<T> clazz;

		public FastJson2JsonRedisSerializer(Class<T> clazz) {
			super();
			this.clazz = clazz;
		}

		@Override
		public byte[] serialize(T t) throws SerializationException {
			if (t == null) {
				return new byte[0];
			}
			return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(Charset.forName("UTF-8"));
		}

		@Override
		public T deserialize(byte[] bytes) throws SerializationException {
			if (bytes == null || bytes.length <= 0) {
				return null;
			}
			String str = new String(bytes, Charset.forName("UTF-8"));
			return (T) JSON.parseObject(str, clazz);
		}
	}

}
